﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;



namespace lib472.API
{
    public class WebApi
    {
        private SourceType sourceType;

        public WebApi(SourceType sourceType)
        {
            this.sourceType = sourceType;
        }

        public T GetDataList<T>(lib472.Model.Mdb model)
        {    
            string Postdata = Clientbuss.Post("/Edi/GetDataList", model);
            if (string.IsNullOrEmpty(Postdata))
            {
                throw new ArgumentNullException(nameof(Postdata));
            }
            return JsonConvert.DeserializeObject<T>(Postdata);
        }

        public string GetApiData(lib472.Model.Mdb model)
        {
            string Postdata = Clientbuss.Post("/Edi/GetDataList", model);

            if (string.IsNullOrEmpty(Postdata))
            {
                throw new ArgumentNullException(nameof(Postdata));
            }

            return Postdata;
        }

        public int Execute(string TableName, string Where, string MethodName)
        {
            
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = Where,
                    MethodName = MethodName,
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = string.Empty,
                };
                return int.Parse(GetApiData(mdb));
           
        }

        public int Execute(string Sql)
        {
            
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = string.Empty,
                    DbType = sourceType,
                    Where = string.Empty,
                    MethodName = "execute",
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = Sql,
                };
            return int.Parse(GetApiData(mdb));
            
        }
        public int Execute(string TableName,  string MethodName,object data)
        {
           
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = string.Empty,
                    MethodName = MethodName,
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = data,
                };
                return int.Parse(GetApiData(mdb));

        
        }

        public  T GetDataList<T>(string TableName,  string MethodName, object data)
        {
      
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = string.Empty,
                    MethodName = MethodName,
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = data
                };
                return GetDataList<T>(mdb);
      
        }


        public  T GetDataList<T>(string TableName, string Where, string MethodName, object data)
        {
           
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = Where,
                    MethodName = MethodName,
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = data
                };
                return GetDataList<T>(mdb);
         
        }

        public  T GetDataList<T>(string TableName)
        {
        
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = string.Empty,
                    MethodName = string.Empty,
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = string.Empty
                };
                return GetDataList<T>(mdb);
         
        }

        public  T GetDataList<T>(string TableName, string Where)
        {
        
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = Where,
                    MethodName = string.Empty,
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = string.Empty
                };
                return GetDataList<T>(mdb);

        }


        public  T GetDataList<T>(string TableName,  string Where, string MethodName)
        {

                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = Where,
                    MethodName = MethodName,
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = string.Empty
                };
                return GetDataList<T>(mdb);
      
        }
        public T GetDataList<T>(string TableName,  string Where, string MethodName, Dictionary<string, List<string>> WhereIn)
        {
       
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = Where,
                    MethodName = MethodName,
                    WhereIn = WhereIn,
                    data = string.Empty
                };
                return GetDataList<T>(mdb);
  
        }

        public List<T> GetData<T>(string TableName,string Where="",string order="",string MethodName="", int limit=0,object data=null)
        {
            lib472.Model.Mdb mdb = new lib472.Model.Mdb
            {
                TableName = TableName,
                DbType = sourceType,
                Where = Where,
                MethodName = MethodName,
                WhereIn = new Dictionary<string, List<string>>(),
                data = data == null ? string.Empty : data  ,
                orderby = order,
                limit = limit
            };

            return GetData<T>(mdb);
        }

        private List<T> GetData<T>(lib472.Model.Mdb model)
        {
            string Postdata = Clientbuss.Post("/Edi/GetDataList", model);
            if (string.IsNullOrEmpty(Postdata))
            {
                throw new ArgumentNullException(nameof(Postdata));
            }
            
            return JsonConvert.DeserializeObject<List<T>>(Postdata);
        }



        public T GetDataObject<T>(string SqlString)
        {
            lib472.Model.Mdb mdb = new lib472.Model.Mdb
            {
                TableName =string.Empty,
                DbType = sourceType,
                Where =string.Empty,
                MethodName = "query",
                WhereIn = new Dictionary<string, List<string>>(),
                data = SqlString,
                orderby = string.Empty,
                limit = 0
            };

            string Postdata = Clientbuss.Post("/Edi/GetDataList", mdb);
            if (string.IsNullOrEmpty(Postdata))
            {
                throw new ArgumentNullException(nameof(Postdata));
            }
            
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(Postdata);
        }


        public T GetDataObject<T>(string TableName, string Where = "", string order = "", string MethodName = "", int limit = 0, object data = null)
        {
            lib472.Model.Mdb mdb = new lib472.Model.Mdb
            {
                TableName = TableName,
                DbType = sourceType,
                Where = Where,
                MethodName = MethodName,
                WhereIn = new Dictionary<string, List<string>>(),
                data = data == null ? string.Empty : data,
                orderby = order,
                limit = limit
            };

            string Postdata = Clientbuss.Post("/Edi/GetDataList", mdb);
            if (string.IsNullOrEmpty(Postdata))
            {
                throw new ArgumentNullException(nameof(Postdata));
            }

            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(Postdata);
        }

        public dynamic GetdynamicList(string TableName)
        {
          
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = string.Empty,
                    MethodName = string.Empty,
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = string.Empty
                };

                string Postdata = Clientbuss.Post("/Edi/GetDataList", mdb);
                if (string.IsNullOrEmpty(Postdata))
                {
                    throw new ArgumentNullException(nameof(Postdata));
                }

                DataTable d= Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(Postdata);

                return JsonConvert.DeserializeObject<dynamic>(Postdata);
;
        }

        public int ExecuteScalar(string Sql)
        {

            lib472.Model.Mdb mdb = new lib472.Model.Mdb
            {
                TableName = string.Empty,
                DbType = sourceType,
                Where = string.Empty,
                MethodName = "scalar",
                WhereIn = new Dictionary<string, List<string>>(),
                data = Sql,
            };
            return int.Parse(GetApiData(mdb));

        }

        public object ExecuteScalar(string TableName, object data)
        {
         
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = string.Empty,
                    MethodName = "scalar",
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = data,
                };
                return GetApiData(mdb);
        
        }

        public int Update(string TableName, object data)
        {
          
                lib472.Model.Mdb mdb = new lib472.Model.Mdb
                {
                    TableName = TableName,
                    DbType = sourceType,
                    Where = string.Empty,
                    MethodName = "execute",
                    WhereIn = new Dictionary<string, List<string>>(),
                    data = data,
                };
                return int.Parse(GetApiData(mdb));

           
        }


        public int Updates(string TableName, object data)
        {

            lib472.Model.Mdb mdb = new lib472.Model.Mdb
            {
                TableName = TableName,
                DbType = sourceType,
                Where = string.Empty,
                MethodName = "updates",
                WhereIn = new Dictionary<string, List<string>>(),
                data = data,
            };
            return int.Parse(GetApiData(mdb));


        }

    }
}
